package cn.lyjuan.android.demofive.frame.realm;

import android.content.DialogInterface;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;

import cn.lyjuan.android.demofive.R;
import cn.lyjuan.android.demofive.databinding.FrameRealmRecyclerActivityBinding;
import cn.lyjuan.android.demofive.databinding.FrameRealmRecyclerAddBinding;
import cn.lyjuan.android.demofive.databinding.FrameRealmRecyclerItemBinding;
import cn.lyjuan.android.mylibrary.context.RealmApplication;
import cn.lyjuan.base.util.StringUtils;
import io.realm.Realm;
import io.realm.RealmResults;

/**
 * 初始化时数据 list 为空
 * Created by chad on 06/11/2017.
 */

public class RecyclerListRealmActivity extends AppCompatActivity
{
    private FrameRealmRecyclerActivityBinding binding;
    private RealmResults<UserModel> list;

    private Realm realm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.frame_realm_recycler_activity);

        realm = RealmApplication.realm();
        list = UserModel.findAll(realm);

        setView();
    }

    protected void setView()
    {
//        binding.recyclerView.setLayoutManager(new PTLLinearLayoutManager(PTLLinearLayoutManager.VERTICAL));
        binding.recyclerView.addItemDecoration(new BaseItemDecoration(this, R.drawable.shape_recycler_divider, 2, 1));
        binding.recyclerView.setAdapter(new RecyclerView.Adapter<DataBindViewHolder>()
        {
            @Override
            public DataBindViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
            {
                FrameRealmRecyclerItemBinding binding = DataBindingUtil.inflate(RecyclerListRealmActivity.this.getLayoutInflater(),
                        R.layout.frame_realm_recycler_item, parent, false);
                return new DataBindViewHolder(binding);
            }

            @Override
            public void onBindViewHolder(final DataBindViewHolder holder, final int position)
            {
                holder.binding.setUser(list.get(position));
                holder.binding.setLongClickListener(new View.OnLongClickListener()
                {
                    @Override
                    public boolean onLongClick(View v)
                    {
                        Snackbar.make(holder.binding.getRoot(), "click >> " + list.get(position).getName(), Snackbar.LENGTH_LONG).show();
                        list.get(position).del();
                        return true;
                    }
                });
            }

            @Override
            public int getItemCount()
            {
                return list.size();
            }
        });

//        binding.recyclerView.setLoadEn(false);
    }

    private static final Integer MENU_ITEM_ADD = 1;

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add(0, MENU_ITEM_ADD, 1, "添加");
        MenuItem item = menu.findItem(MENU_ITEM_ADD);
        item.setVisible(true);
        return true;
    }

    private AlertDialog dialog;

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        if (item.getItemId() == MENU_ITEM_ADD)
        {
            if (null == dialog)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);

                final FrameRealmRecyclerAddBinding addBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame_realm_recycler_add, null, false);

                builder.setTitle("添加").setView(addBinding.getRoot())
                        .setPositiveButton("确定", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                String name = addBinding.name.getText().toString();
                                String age = addBinding.age.getText().toString();
                                // 添加
                                if (StringUtils.isNull(name) || StringUtils.isNull(age))
                                    Snackbar.make(null, "属性不能为空", Snackbar.LENGTH_SHORT).show();

                                // 添加用户
                                UserModel.add(realm, name, Integer.parseInt(age));
                            }
                        });

                dialog = builder.create();
            }

            dialog.show();
            return true;
        }

        return false;
    }

    public class DataBindViewHolder extends RecyclerView.ViewHolder
    {
        private FrameRealmRecyclerItemBinding binding;

        public DataBindViewHolder(final FrameRealmRecyclerItemBinding binding)
        {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
